package com.zhengyisky.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhengyisky.entity.RequestEnity;
import com.zhengyisky.utils.RSAUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 八维服务器
 */
@Controller
public class BankController {

    @Value("${aibank.platformNo}")
    private String platformNo;

    @Value("${key.publicKey}")
    private String publicKey;

    // 初始化个人开户
    @PostMapping("/initPersonRegisterParam/{mobile}")
    @ResponseBody
    public RequestEnity initPersonRegisterParam(@PathVariable String mobile) throws Exception {
        RequestEnity requestEnity = new RequestEnity();
        requestEnity.setServiceName("PERSONAL_REGISTER_EXPAND");// 接口名称
        requestEnity.setPlatformNo(platformNo);// 平台编号
        requestEnity.setUserDevice("MOBILE");//终端类型
        requestEnity.setKeySerial(1);//证书序号
        // 具体接口的参数，赋值给reqData
        Map map = new HashMap();
        map.put("platformUserNo",mobile);
        map.put("requestNo", UUID.randomUUID().toString());
        map.put("userRole","BORROWERS");
        map.put("checkType","LIMIT");
        map.put("commission","10");
        map.put("redirectUrl","http://localhost:5001/resultPersonRegister");
        String reqData = JSONObject.toJSONString(map);
        requestEnity.setReqData(reqData);
        //解密和解密(签名的处理)。签名是根据reqData得到
        requestEnity.setSign(RSAUtils.encrypt(reqData.substring(0,100),publicKey));
        return requestEnity;
    }

    //异步回调
    @GetMapping("/notifyResult")
    @ResponseBody
    public String notifyResult(HttpServletRequest request){
        System.out.println("接收到异步回调");

        String serviceName = request.getParameter("serviceName");
        System.out.println(serviceName);
        //验签

        //解析respData


        return "SUCCESS";
    }

    //返回的结果
    @GetMapping("/resultPersonRegister")
    public String resultPersonRegister(HttpServletRequest request){
        String sign = request.getParameter("sign");
        System.out.println(sign);
        String respData = request.getParameter("respData");
        System.out.println(respData);
        //1、根据私钥+签名---》解密
        //2、判断解密的结果和respData是否一致
        //3、从respData中取出code
        //4、判断code是不是0

        // 修改用户状态   已开户


        return "redirect:/accountSuccess.html";
    }
    @GetMapping("/resultCompanyRegister")
    public String resultCompanyRegister(HttpServletRequest request){
        String sign = request.getParameter("sign");
        System.out.println(sign);
        String respData = request.getParameter("respData");
        System.out.println(respData);
        //1、根据私钥+签名---》解密
        //2、判断解密的结果和respData是否一致
        //3、从respData中取出code
        //4、判断code是不是0

        // 修改用户状态   已开户


        return "redirect:/companySuccess.html";
    }

    // 企业账号
    @PostMapping("/initCompanyRegisterParam/{mobile}")
    @ResponseBody
    public RequestEnity initCompanyRegisterParam(@PathVariable String mobile) throws Exception {
        RequestEnity requestEnity = new RequestEnity();
        requestEnity.setServiceName("ENTERPRISE_REGISTER");// 接口名称
        requestEnity.setPlatformNo(platformNo);// 平台编号
        requestEnity.setUserDevice("MOBILE");//终端类型
        requestEnity.setKeySerial(1);//证书序号
        // 具体接口的参数，赋值给reqData
        Map map = new HashMap();
        map.put("platformUserNo",mobile);
        map.put("requestNo", UUID.randomUUID().toString());
        map.put("userRole","BORROWERS");
        map.put("org_no","张三");
        map.put("redirectUrl","http://localhost:5001/resultPersonRegister");
        String reqData = JSONObject.toJSONString(map);
        requestEnity.setReqData(reqData);
        //解密和解密(签名的处理)。签名是根据reqData得到
        requestEnity.setSign(RSAUtils.encrypt(reqData.substring(0,100),publicKey));
        return requestEnity;
    }
    // 充值
    @PostMapping("/initRechargeParam/{mobile}")
    @ResponseBody
    public RequestEnity initRechargeParam(@PathVariable String mobile) throws Exception {
        RequestEnity requestEnity = new RequestEnity();
        requestEnity.setServiceName("RECHARGE");// 接口名称-充值
        requestEnity.setPlatformNo(platformNo);// 平台编号
        requestEnity.setUserDevice("MOBILE");//终端类型
        requestEnity.setKeySerial(1);//证书序号
        // 具体接口的参数，赋值给reqData
        Map map = new HashMap();
        map.put("platformUserNo",mobile);// 平台用户编号 这个平台用户编号我们用的是手机号
        map.put("requestNo", UUID.randomUUID().toString());//请求流水号
        // 传充值方式和银行  用快捷支付
        map.put("expectPayCompany","BAIXIN");//支付公司
        map.put("rechargeWay","SWIFT");//支付方式
        map.put("bankcode","FJIB");//银行编码
        map.put("payType","B2C");//网银类型
        map.put("redirectUrl","http://localhost:5001/resultPersonRegister");
        String reqData = JSONObject.toJSONString(map);
        requestEnity.setReqData(reqData);
        //解密和解密(签名的处理)。签名是根据reqData得到
        requestEnity.setSign(RSAUtils.encrypt(reqData.substring(0,100),publicKey));
        return requestEnity;
    }
    // 银行卡还款 -直连接口
    @PostMapping("/initBankCardRegisterParam/{mobile}")
    @ResponseBody
    public RequestEnity initBankCardRegisterParam(@PathVariable String mobile) throws Exception {
        RequestEnity requestEnity = new RequestEnity();
        requestEnity.setServiceName("DIRECT_RECHARGE");// 接口名称
        requestEnity.setPlatformNo(platformNo);// 平台编号
        requestEnity.setUserDevice("MOBILE");//终端类型
        requestEnity.setKeySerial(1);//证书序号
        // 具体接口的参数，赋值给reqData
        Map map = new HashMap();
        map.put("platformUserNo",mobile);// 平台用户编号 这个平台用户编号我们用的是手机号
        map.put("requestNo", UUID.randomUUID().toString());//请求流水号
        map.put("proxyPretransRequestNo","12343432");//绑定卡还款预处理流水号
        map.put("proxyAppendRequestNo","3432423");
        map.put("amount","10000");
        map.put("expectPayCompany","百信支付");//支付公司
        map.put("rechargeWay","快捷支付");//支付方式
        String reqData = JSONObject.toJSONString(map);
        requestEnity.setReqData(reqData);
        //解密和解密(签名的处理)。签名是根据reqData得到
        requestEnity.setSign(RSAUtils.encrypt(reqData.substring(0,100),publicKey));
        return requestEnity;
    }
}
